package com.explam.springboot.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.explam.springboot.common.ResultUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.explam.springboot.service.ITypeService;
import com.explam.springboot.entity.Type;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-03-25
 */
@RestController
@RequestMapping("/admin/type")
public class TypeController {

    @Resource
    private ITypeService typeService;

    //新增和修改
    @PostMapping("/update")
    public ResultUtil update(@RequestBody Type type){
        typeService.saveOrUpdate(type);
        return ResultUtil.success();
    }

    //新增和修改
    @PostMapping("/save")
    public ResultUtil save(@RequestBody Type type){
        typeService.save(type);
        return ResultUtil.success();
    }

        //删除
    @GetMapping("/delete/id={id}")
    public ResultUtil delete(@PathVariable Integer id) {
        typeService.removeById(id);
        return ResultUtil.success();
    }

    //批量删除
    @PostMapping("/del/batch")
    public ResultUtil deleteBatch(@RequestBody List<Integer> ids){
         typeService.removeByIds(ids);
        return ResultUtil.success();
    }

        //查询所有数据
    @GetMapping("/all")
    public ResultUtil findAll() {
        return ResultUtil.success(typeService.list());
    }

        //根据id查询
    @GetMapping("/get-type/id={id}")
    public ResultUtil findOne(@PathVariable Integer id) {
        return ResultUtil.success(typeService.getById(id));
    }

        //分页查询
    @GetMapping("/page")
    public ResultUtil findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return ResultUtil.success(typeService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }
}

